package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.ProductMultiUnitAddDTO;
import com.xbongbong.paas.pojo.dto.ProductMultiUnitDetailDTO;
import com.xbongbong.paas.pojo.dto.ProductMultiUnitListDTO;
import com.xbongbong.paas.pojo.dto.ProductMultiUnitSortUpdateDTO;
import com.xbongbong.paas.pojo.dto.ProductMultiUnitUpdateDTO;
import com.xbongbong.paas.pojo.vo.ProductMultiUnitDetailVO;
import com.xbongbong.paas.pojo.vo.ProductMultiUnitListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.ProductMultiUnitService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 产品多单位
 *
 * @author lee
 * @version v1.0
 * @date 2020/9/9 11:33
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/productMultiUnit")
public class ProductMultiUnitController {

    @Resource
    private ProductMultiUnitService productMultiUnitService;

    /**
     * 产品多单位列表查询
     *
     * @param productMultiUnitListDTO
     * @param br
     * @return java.lang.String
     * @author lee
     * @date 2020/9/9 13:45
     * @since v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String search(@RequestBody @Valid ProductMultiUnitListDTO productMultiUnitListDTO, BindingResult br) {
        XbbResponse<ProductMultiUnitListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductMultiUnitListVO productMultiUnitListVO = productMultiUnitService.list(productMultiUnitListDTO);
                response = new XbbResponse<>(productMultiUnitListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 产品多单位详情查询
     *
     * @param productMultiUnitDetailDTO
     * @param br
     * @return java.lang.String
     * @author lee
     * @date 2020/9/9 13:45
     * @since v1.0
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid ProductMultiUnitDetailDTO productMultiUnitDetailDTO, BindingResult br) {
        XbbResponse<ProductMultiUnitDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductMultiUnitDetailVO productMultiUnitDetailVO = productMultiUnitService.detail(productMultiUnitDetailDTO);
                response = new XbbResponse<>(productMultiUnitDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 产品多单位新增
     *
     * @param productMultiUnitAddDTO
     * @param br
     * @return java.lang.String
     * @author lee
     * @date 2020/9/9 13:45
     * @since v1.0
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid ProductMultiUnitAddDTO productMultiUnitAddDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = productMultiUnitService.add(productMultiUnitAddDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 产品多单位更新
     *
     * @param productMultiUnitUpdateDTO
     * @param br
     * @return java.lang.String
     * @author lee
     * @date 2020/9/9 13:45
     * @since v1.0
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid ProductMultiUnitUpdateDTO productMultiUnitUpdateDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = productMultiUnitService.update(productMultiUnitUpdateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 产品多单位排序更新
     *
     * @param productMultiUnitSortUpdateDTO
     * @param br
     * @return java.lang.String
     * @author lee
     * @date 2020/9/9 13:45
     * @since v1.0
     */
    @RequestMapping(value = "/sort/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sortUpdate(@RequestBody @Valid ProductMultiUnitSortUpdateDTO productMultiUnitSortUpdateDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = productMultiUnitService.sortUpdate(productMultiUnitSortUpdateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 产品多单位保存
     *
     * @param productMultiUnitDetailDTO
     * @param br
     * @return java.lang.String
     * @author lee
     * @date 2020/9/9 13:45
     * @since v1.0
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid ProductMultiUnitDetailDTO productMultiUnitDetailDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = productMultiUnitService.delete(productMultiUnitDetailDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
